home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_4 / a4_1c.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  2.4 KB  |  89 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 4.1 (Evaluation of a Taylor Series).
  9. % Section    4.1,    Taylor Series and Calculation of Functions, Page 203
  10. echo on; clc; format long; hold off; clear
  11.  
  12. % This program animates Taylor approximations.
  13.  
  14. %      Pn(x) = c(1) + c(2)x + c(2)x^2 + ... + c(n+1)x^n
  15.  
  16. %where the degree n of approximation is large  (n ~ 25).
  17.  
  18. % Coefficient lists for several functions have been
  19.  
  20. % stored in M-files named;   zcos  zsin  ztan  zexp
  21.  
  22. % zacos  zasin  zatan  zcosh   zsinh   zsqrt   zlog
  23.  
  24. % zsqrt4   zinv   zemx2d2   zcosde   zsinde   zlogq
  25.  
  26. pause    % Press any key continue.
  27.  
  28. clc;
  29. %       Approximations for  tan(x)
  30.  
  31. % Issue the command  ztan  to load the coefficients
  32.  
  33. % into the array  C.  The function name is loaded
  34.  
  35. % into the variable fun, the degree is loaded into N.
  36.  
  37. % The endpoints of [a,b] are loaded into  a  and b.
  38.  
  39. % Load the Taylor coefficients.
  40.  
  41. [fun,dfun,ifun,x0,m,C,Ax] = ztan;
  42.  
  43. pause     % Press any key continue.
  44. clc;
  45. a = Ax(1,1);    % You can change the endpoint a.
  46. b = Ax(1,2);    % You can change the endpoint b.
  47.  
  48. n = m;          % You can change the degree n.
  49. Mx1 = 'The function is  f(x) = ';
  50. Mx2 = 'The interval is  ';
  51. Mx3 = 'Pn(x) = c(1)x^n + c(2)x^(n-1) + ... + c(n)x + c(n+1)';
  52. Mx4 = 'The degree is up to  n = ';
  53. Mx5 = ', and the coefficients list  C  is:';
  54. clc,format short e,...
  55. disp(''),disp([Mx1,fun]),...
  56. disp([Mx2,'[',num2str(a),' , ',num2str(b),']']),...
  57. disp(Mx3),disp([Mx4,num2str(n),Mx5]),...
  58. for i=1:5:n+1, disp(C([i:min(i+4,n+1)])'); end
  59.  
  60. pause    % Press any key continue.
  61.  
  62. clc; clg;
  63. h = (b-a)/200;
  64. X = a:h:b;
  65. x = X;
  66. Y = eval(fun);
  67. axis(Ax(1,:));...
  68. plot(X,Y,'-g');...
  69. title(['Comparison of ',fun,' and Pk(x).']);...
  70. xlabel('x');...
  71. ylabel('y');...
  72. hold on;...
  73. plot([a b],[0 0],'b',[0 0],[-10000 10000],'b');...
  74. for k = 3:n+1,...
  75.   Ck = flipud(C);...
  76.   Ck = Ck(1:k);...
  77.   Ck = flipud(Ck);...
  78.   P = polyval(Ck,X);...
  79.   plot(X,P,'-r');...
  80. end;...
  81. hold off;...
  82. shg; pause    % Press any key to continue.
  83.  
  84. clc,format short e,...
  85. disp(''),disp([Mx1,fun]),...
  86. disp([Mx2,'[',num2str(a),' , ',num2str(b),']']),...
  87. disp(Mx3),disp([Mx4,num2str(n),Mx5]),...
  88. for i=1:5:n+1, disp(C([i:min(i+4,n+1)])'); end
  89.